﻿using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
 using System.Web.UI.Design;
using System;

namespace Grishko.UIControls
{
  [TypeConverter(typeof(ExpandableObjectConverter))]
  public class MenuItemData
  {
    public MenuItemData()
    {
       

    }

    //Override this method to display just MenuItemData instead of fully qualified type 
    //in the custom collection editor
    public override string ToString()
    {
      return "MenuItemData";
    }

    [NotifyParentProperty(true)]
    public string Title {get; set; }

    [NotifyParentProperty(true)]
    public string Url { get; set; }

    [NotifyParentProperty(true)]
    public string ImageUrl {get; set; }

    [NotifyParentProperty(true)]
    public string Target { get; set; }
  }

  public class TemplateMenuDesigner : ControlDesigner
    {
        TemplateGroupCollection templateGroupCol = null;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            if (!(component is TemplateMenu))
            {
                throw new ArgumentException("Component must be a TemplateMenu control for this custom designer."
                   , "component");
            }
            else
            {
                SetViewFlags(ViewFlags.TemplateEditing, true);
            }
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                if (templateGroupCol == null)
                {
                    // Get the base collection
                    templateGroupCol = base.TemplateGroups;

                    TemplateGroup templateGroup;
                    TemplateDefinition templateDef;
                    TemplateMenu ctl;

                    //Get reference to the component as TemplateMenu
                    ctl = (TemplateMenu)Component;

                    //Create Template Group
                    templateGroup = new TemplateGroup("TemplateMenu Templates");

                    //Header
                    templateDef = new TemplateDefinition(this, "Header",
                        ctl, "HeaderTemplate", false);
                    templateGroup.AddTemplateDefinition(templateDef);

                    //Separator
                    templateDef = new TemplateDefinition(this, "Separator",
                         ctl, "SeparatorTemplate", false);
                    templateGroup.AddTemplateDefinition(templateDef);

                    //Footer
                    templateDef = new TemplateDefinition(this, "Footer",
                        ctl, "FooterTemplate", false);
                    templateGroup.AddTemplateDefinition(templateDef);

                    // Add the TemplateGroup to the TemplateGroupCollection
                    templateGroupCol.Add(templateGroup);
                }

                return templateGroupCol;
            }
        }

        public override string GetDesignTimeHtml()
        {
            //Return configuraiton instructions if no templates are set.
            if ((null == ((TemplateMenu)Component).HeaderTemplate) &&
               (null == ((TemplateMenu)Component).SeparatorTemplate) &&
               (null == ((TemplateMenu)Component).FooterTemplate))
            {
                return CreatePlaceHolderDesignTimeHtml("Click here and use the task menu to edit  TemplateMenu Header, Footer, and Seperator template properties. " +
                 "<br>A default template is used at run-time if the separator Template is not specified at design-time." +
                 "<br>The Header and Footer templates are optional.");
            }

            //return configured html
            string designTimeHtml = String.Empty;
            try
            {
                ((TemplateMenu)Component).DataBind();
                designTimeHtml = base.GetDesignTimeHtml();
            }
            catch (Exception e)
            {
                designTimeHtml = GetErrorDesignTimeHtml(e);
            }
            return designTimeHtml;
        }

        protected override string GetErrorDesignTimeHtml(Exception e)
        {

            return CreatePlaceHolderDesignTimeHtml("There was an error rendering the TemplateMenu control." +
               "<br>Exception: " + e.Source + "  Message: " + e.Message);
        }

        public override bool AllowResize
        {
            get
            {
                bool templateExists = null != ((TemplateMenu)Component).HeaderTemplate ||
                                                  null != ((TemplateMenu)Component).SeparatorTemplate ||
                                                  null != ((TemplateMenu)Component).FooterTemplate;
                return templateExists || InTemplateMode;
            }
        }
    }

  public class BasicTemplateContainer : WebControl, INamingContainer
  {
      public BasicTemplateContainer()
          : base(HtmlTextWriterTag.Span)
      {
          this.BorderWidth = 2;
          this.BorderStyle = BorderStyle.Outset;
      }
  }

  public class SeperatorTemplateContainer : WebControl, INamingContainer
  {
      public SeperatorTemplateContainer()
          : base(HtmlTextWriterTag.Span)
      {
      }
  }
 

 [ToolboxData("<{0}:templatemenu runat=server></{0}:templatemenu>"),
  Designer(typeof(TemplateMenuDesigner))]
 public class TemplateMenu : CompositeControl
  {
    private ArrayList menuData;
    public TemplateMenu()
      : base()
    {
      menuData = new ArrayList()  //Uses new C# 3.0 Object and Collection Initializers
      {
        new MenuItemData{Title="Apress", Url="http://www.apress.com"},
        new MenuItemData{Title="Microsoft", Url="http://www.microsoft.com"},
        new MenuItemData{Title="ASP.Net", Url="http://asp.net"}
      };
    }

    private ITemplate headerTemplate;
    [Browsable(false), Description("The header template"), PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(BasicTemplateContainer))]
    public ITemplate HeaderTemplate
    {
      get
      {
        return headerTemplate;
      }
      set
      {
        headerTemplate = value;
      }
    }

    private ITemplate footerTemplate;
    [Browsable(false), Description("The footer template"), PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(BasicTemplateContainer))]
    public ITemplate FooterTemplate
    {
      get
      {
        return footerTemplate;
      }
      set
      {
        footerTemplate = value;
      }
    }

    private ITemplate separatorTemplate;
    [Browsable(false), Description("The separator template"), PersistenceMode(PersistenceMode.InnerProperty),
   TemplateContainer(typeof(SeperatorTemplateContainer))]
    public ITemplate SeparatorTemplate
    {
      get
      {
        return separatorTemplate;
      }
      set
      {
        separatorTemplate = value;
      }
    }

    private void CreateControlHierarchy()
    {
      if (HeaderTemplate != null)
      {
        BasicTemplateContainer header = new BasicTemplateContainer();
        HeaderTemplate.InstantiateIn(header);
        Controls.Add(header);
      }

      //int count = menuData.Count;
      //for (int index = 0; index < count; index++)
      //{
      //  MenuItemData itemdata = (MenuItemData)menuData[index];

      //  HyperLink link = new HyperLink() { Text = itemdata.Title, NavigateUrl = itemdata.Url, ImageUrl = itemdata.ImageUrl, Target = itemdata.Target };
      //  Controls.Add(link);

      //  if (index != count - 1)
      //  {
      //    if (SeparatorTemplate != null)
      //    {
      //      SeperatorTemplateContainer separator = new SeperatorTemplateContainer();
      //      SeparatorTemplate.InstantiateIn(separator);
      //      Controls.Add(separator);
      //    }
      //    else
      //    {
      //      Controls.Add(new LiteralControl(" | "));
      //    }
      //  }
      //}

      if (FooterTemplate != null)
      {
        BasicTemplateContainer footer = new BasicTemplateContainer();
        FooterTemplate.InstantiateIn(footer);
        Controls.Add(footer);
      }
    }

    override protected void CreateChildControls()
    {
      Controls.Clear();
      CreateControlHierarchy();
    }

    public override ControlCollection Controls
    {
      get
      {
        EnsureChildControls();
        return base.Controls;
      }
    }

    public override void DataBind()
    {
      CreateChildControls();
      ChildControlsCreated = true;

      base.DataBind();
    }
  }

   
}


